package fr.upstlse.glre.client.administration.document.libelle;

import java.util.Collection;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.binder.DataListBinder;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.DataList;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;

import fr.upstlse.glre.client.administration.component.Center;
import fr.upstlse.glre.client.administration.document.libelle.action.LibelleSelectionAction;
import fr.upstlse.glre.client.constante.AdminConstante;
import fr.upstlse.glre.client.constante.fields.LibelleConstante;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.depot.LibelleVOGWT;
import fr.upstlse.glre.client.widgets.modelData.LibelleModelData;

public class ModificationLibellePanel extends Composite {
	/**
	 * Panneau offrant un certain nombre de widgets. Il contient le panneau de
	 * contenu.
	 */
	private ContentPanel modificationPanel;

	/**
	 * Panneau FORM contenant le profil au format formulaire pour l'édition.
	 */
	private DataList modificationList;

	private ContentPanel libelleModificationPanel;

	private Collection<LibelleVOGWT> libelleVOGWTs;

	private static ModificationLibellePanel instance;
	
	public ModificationLibellePanel() {
		ModificationLibellePanel.setInstance(this);

		createModificationPanel();
		createLibelleModificationPanel();
		modificationList = new DataList();

		modificationPanel.add(modificationList);
		modificationPanel.add(libelleModificationPanel);
		initWidget(modificationPanel);
	}

	private void createModificationPanel() {
		modificationPanel = new ContentPanel();
		modificationPanel.setAutoWidth(true);
		modificationPanel.setWidth(500);
		modificationPanel
				.setId(AdminConstante.LIBELLE_MODIFICATION_PANEL_ID.getValue());
		modificationPanel.setBorders(true);
		modificationPanel.setCollapsible(true);
		modificationPanel.collapse();

		// Draggable panel
		Draggable lDraggable = new Draggable(modificationPanel,
				modificationPanel.getHeader());
		lDraggable.setContainer(Center.getInstance());

		modificationPanel
				.setHeading(AdminConstante.LIBELLE_MODIFICATION_PANEL_HEADER_TEXT.getValue());

		modificationPanel.addListener(Events.Expand, new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				createModificationList();
			}
		});
	}

	private void createModificationList() {
		if (libelleVOGWTs == null) {
			final MessageBox lAttente = MessageBox.alert("Information",
					"Chargement des libellés en cours...", null);
			AsyncCallback<Collection<LibelleVOGWT>> lCallback = new AsyncCallback<Collection<LibelleVOGWT>>() {

				public void onFailure(Throwable pCaught) {
					lAttente.hide();
					MessageBox.alert("Erreur", "Erreur getAllLibelle "
							+ pCaught.getMessage(), null);
				}

				public void onSuccess(Collection<LibelleVOGWT> pResult) {
					lAttente.hide();
					libelleVOGWTs = pResult;

					final ListStore<LibelleModelData> lStore = new ListStore<LibelleModelData>();
					lStore.add(LibelleModelData.toLibelleModelDatas(libelleVOGWTs));
					DataListBinder<LibelleModelData> binder = new DataListBinder<LibelleModelData>(
							modificationList, lStore);
					binder
							.setDisplayProperty(LibelleConstante.MODELDATA_INTITULE.getValue());
					binder
							.setStringProvider(new ModelStringProvider<ModelData>() {

								public String getStringValue(ModelData model,
										String property) {
									String lTexte = (String) model
											.get(LibelleConstante.MODELDATA_INTITULE.getValue());
									return lTexte;
								}

							});
					binder.init();

					modificationList.addListener(Events.SelectionChange,
							new LibelleSelectionAction());

					Info.display("Information",
							"Succès du chargement des libellés", "");
				}

			};
			ControlerUtil.getControlerServiceAsyncInstance().getAllLibelles(lCallback);
		}
	}

	private void createLibelleModificationPanel() {
		libelleModificationPanel = new ContentPanel();
		libelleModificationPanel.setAutoWidth(true);
		libelleModificationPanel.setWidth(500);
		libelleModificationPanel
				.setId(AdminConstante.LIBELLES_LIBELLE_MODIFICATION_PANEL_ID.getValue());
		libelleModificationPanel.setBorders(true);
		libelleModificationPanel.setHeaderVisible(false);
	}

	public ContentPanel getModificationPanel() {
		return modificationPanel;
	}

	public void setModificationPanel(ContentPanel modificationPanel) {
		this.modificationPanel = modificationPanel;
	}

	public DataList getModificationList() {
		return modificationList;
	}

	public void setModificationList(DataList modificationList) {
		this.modificationList = modificationList;
	}

	public ContentPanel getUserModificationPanel() {
		return libelleModificationPanel;
	}

	public void setUserModificationPanel(ContentPanel userModificationPanel) {
		this.libelleModificationPanel = userModificationPanel;
	}

	public static ModificationLibellePanel getInstance() {
		if (instance == null) {
			instance = new ModificationLibellePanel();
		}

		instance.getModificationPanel().layout();
		return instance;
	}

	public static void setInstance(ModificationLibellePanel pModificationLibellePanel) {
		instance = pModificationLibellePanel;
	}
}
